package logic

import (
	"context"
	"fmt"
	"novel/model/mysql"

	"novel/novelrpc/internal/svc"
	"novel/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type NovelAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewNovelAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NovelAddLogic {
	return &NovelAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *NovelAddLogic) NovelAdd(in *novelrpc.NovelAddRequest) (*novelrpc.NovelAddResponse, error) {
	// todo: add your logic here and delete this line

	var novel mysql.Novel
	if in.Name == "" {
		return nil, fmt.Errorf("标题不允许为空")
	}
	if in.Types == "" {
		return nil, fmt.Errorf("分类不允许为空")
	}
	if in.Ifor == "" {
		return nil, fmt.Errorf("简介不允许为空")
	}
	novel = mysql.Novel{
		Name:   in.Name,
		Types:  in.Types,
		Tile:   in.Tile,
		Ifor:   in.Ifor,
		UserId: int(in.UserId),
	}
	mysql.DB.Debug().Create(&novel)

	return &novelrpc.NovelAddResponse{
		NovelId: int64(novel.ID),
	}, nil
}
